package com.youlu.campus.service.commission;

import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.commission.DistributorChannelActivity;
import com.youlu.campus.entity.commission.dto.DistributorBuildLinkDTO;
import com.youlu.campus.entity.commission.dto.DistributorChannelActivityDTO;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.platform.AppUserShareActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class DistributorChannelActivityService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private AppUserShareActivityService appUserShareActivityService;

    @Autowired
    private ActivityService activityService;


    public Page<DistributorChannelActivity> list(DistributorChannelActivityDTO dto){
        PageRequest pageRequest = PageRequest.of(dto.getPageNo() - 1, dto.getPageSize());
        Query query = new Query();
        query.addCriteria(Criteria.where("channelId").is(dto.getChannelId()));
        long count = this.mongoTemplate.count(query, DistributorChannelActivity.class);
        if(count ==0){
            return PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> count);
        }
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));
        Sort sort=Sort.by(Sort.Direction.DESC, "createdTime");
        query.with(sort);
        List<DistributorChannelActivity> list = this.mongoTemplate.find(query.with(pageRequest), DistributorChannelActivity.class);
        list.stream().forEach(distributorChannelActivity -> {
            String activityId = distributorChannelActivity.getActivityId();
            ActivityInfo activityInfo = activityService.findOne(activityId);
            if(Objects.nonNull(activityInfo)){
                distributorChannelActivity.setActivityName(activityInfo.getName());
            }
            distributorChannelActivity.setLinkUrl(getChannelLinkByChannelIdAndActivityId(distributorChannelActivity.getChannelId(),activityId));
        });
        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    public String getChannelLinkByChannelIdAndActivityId(String channelId,String activityId){
        return appUserShareActivityService.getChannelLinkByChannelIdAndActivityId(channelId,activityId);
    }
    public DistributorChannelActivity findByChannelIdAndActivityId(String channelId,String activityId){
        Query query = new Query();
        query.addCriteria(Criteria.where("channelId").is(channelId));
        query.addCriteria(Criteria.where("activityId").is(activityId));
        return   this.mongoTemplate.findOne(query, DistributorChannelActivity.class);
    }

    public void save(DistributorChannelActivity distributorChannelActivity){
        String activityId = distributorChannelActivity.getActivityId();
        String channelId = distributorChannelActivity.getChannelId();
        if(StringUtils.isAnyBlank(activityId,channelId)){
            log.error("【渠道活动链接】新增或修改参数为空 channelId：{},activityId:{}",channelId,activityId);
            return;
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("channelId").is(channelId));
        query.addCriteria(Criteria.where("activityId").is(activityId));
        Date now = new Date();
        Update update = new Update();
        update.setOnInsert("createdTime",now);
        update.set("updatedTime",now);
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.upsert(Boolean.TRUE);
        findAndModifyOptions.returnNew(Boolean.TRUE);
        this.mongoTemplate.findAndModify(query, update, findAndModifyOptions, DistributorChannelActivity.class);
        DistributorBuildLinkDTO distributorBuildLink = new DistributorBuildLinkDTO();
        distributorBuildLink.setUserId(channelId);
        distributorBuildLink.setActivityId(activityId);
        distributorBuildLink.setShareLinkType(3);
        distributorBuildLink.setPageType(1);
        appUserShareActivityService.buildChannelLink(distributorBuildLink);
    }

}
